﻿using UnityEngine;
using System.Collections;
/// <summary>
/// 英雄控制
/// </summary>
public class Hero : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        GameCtrl.restHandler += restart;
        startpos = pos;
    }
    Vector3 startpos;
    /// <summary>
    /// 位置存取
    /// </summary>
    public  Vector3 pos
    {
        get
        {
            return myTran.position;
        }
        set
        {
            myTran.position = value;
        }
    }
    public static Hero Inst;
    Transform myTran;
    TweenPosition upanddown;
    void Awake()
    {
        upanddown = GetComponent<TweenPosition>();
        Inst = this;
        myTran = transform;
    }
    public void up()
    {
        if(pos.y >= 4.722f)return; //高度限制

        CancelInvoke("down");
        isup = true;
        Invoke("down", 0.25f);
    }
    void down()
    {
        isup = false;
        rigidbody2D.WakeUp();
    }
    bool isup = false;
    public float uppower;

    public AudioClip SFX_up;
    // Update is called once per frame

    Vector3 rotateup = (new Vector3(0, 0, 390));
    Vector3 rotatedown = (new Vector3(0, 0, 270));
    float t = 0.25f;

    void Update()
    {
        if(isup)
        {
            rigidbody2D.Sleep();
            pos += Vector3.up * uppower * Time.deltaTime;
            t -= Time.deltaTime * 10;

            if(t < 0)
                t = 0;
        }

        myTran.eulerAngles = Vector3.Lerp(rotateup, rotatedown, t);

        if(GameCtrl.gameState == GameState.ready)
        {
            t = 0.25f;
        }

        if(GameCtrl.gameState == GameState.gameing)
        {
            t += Time.deltaTime;

            if(t > 1)
                t = 1;

            if(Input.GetMouseButtonUp(0))
            {
                upanddown.enabled = false;
                Hero.Inst.up();
                GameCtrl.Inst.playSFX(SFX_up);
            }
        }
        else if(GameCtrl.gameState != GameState.dieing)
        {
            rigidbody2D.Sleep();
        }
        else
        {
            t += Time.deltaTime * 1.5f;
        }
    }


    public AudioClip SFX_hit;
    bool SFX_yet = false;//一次性控制
    void restart()
    {
        rigidbody2D.Sleep();
        pos = startpos;
        SFX_yet = false;
        upanddown.enabled = true;
    }
    public AudioClip SFX_die;
    void OnTriggerEnter2D(Collider2D other)
    {
        //镜头抖动
        if(!SFX_yet)
        {
            GameCtrl.Inst.playSFX(SFX_hit);
            SFX_yet = true;
            iTween.ShakePosition(Camera.main.gameObject, new Vector3(0.2f, 0.2f), 0.1f);
        }

        if(other.gameObject.tag == "zhuzi")
        {
            other.enabled = false;
            GameCtrl.gameState = GameState.dieing;
            //bug.有时会2次播放.
            GameCtrl.Inst.playSFX(SFX_die, 0.2f);
            Debug.Log("todo.有时会两次播放!");
        }

        if(other.gameObject.name == "dadi")
        {
            GameCtrl.Inst.gameoverOnece();
        }
    }
}
